﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformUpperDataCollections
{
    [DefaultEvent("PageChanged")]//默认事件
    public partial class UPager : UserControl
    {
        public UPager()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount { get; set; }
        /// <summary>
        /// 每页显示数据条数
        /// </summary>
        public int PageSize { get; set; } = 10;
        /// <summary>
        /// 当前页 
        /// </summary>
        public int PageIndex { get; set; } = 1;
       
        /// <summary>
        /// 总共多少页
        /// </summary>
        public int PageCount
        {
            get
            {
                var pgCount = 0;
                if (RecordCount == 0)
                    pgCount = 0;
                else
                {
                    if (RecordCount % PageSize > 0)
                    {
                        pgCount = RecordCount / PageSize + 1;
                    }
                    else
                    {
                        pgCount = RecordCount / PageSize;
                    }
                }
                return pgCount;
            }
        }

        private void InitPageInfo()
        {
            /*if (RecordCount > 0)
            {
                if (CurrentPage > PageCount)
                { 
                    CurrentPage = PageCount;
                }
            }*/
            if (PageIndex == 1)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                if (PageIndex == PageCount)//总共只有一页
                {
                    btnNext.Enabled = false;
                    btnLast.Enabled = false;
                    btnGo.Enabled = false;
                }
                else
                {
                    btnNext.Enabled = true;
                    btnLast.Enabled = true;
                    btnGo.Enabled = true;
                }
            }
            else if (PageIndex > 1)
            {
                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
                //不是第一页 也不是最后一页
                if (PageIndex < PageCount)
                {
                    btnNext.Enabled = true;
                    btnLast.Enabled = true;
                }
                else
                {//如果是最后一夜
                    btnNext.Enabled = false;
                    btnLast.Enabled = false;
                }
                btnGo.Enabled = true;
            }
            //设置不可用按钮背景色
            foreach (Control c in this.Controls)
            {
                if (c is Button && c.Enabled == false)
                {
                    c.BackColor = Color.DarkGray;
                }
                else {
                    c.BackColor = Color.Blue;
                }
            }
            txtPage.Text = PageIndex.ToString();
            lblPageInfo.Text = $"共{RecordCount}条记录，共{PageCount}页，当前是第{PageIndex}页";
        }
        /// <summary>
        /// 定义委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void PageHandler(object sender, EventArgs e);
        /// <summary>
        /// 声明事件
        /// </summary>
        public event PageHandler PageChanged;
        //触发翻页事件
        private void OnPageChanged()
        {
            PageChanged?.Invoke(this,new EventArgs());
            InitPageInfo();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (PageIndex > 1)
            {
                PageIndex = 1;//翻到第一页
                OnPageChanged();//重新去执行重新
            }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (PageIndex > 1)
            {
                PageIndex = PageIndex - 1;//向前翻一页
                OnPageChanged();//重新去执行重新
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (PageIndex < PageCount)
            {
                PageIndex = PageIndex + 1;//向前翻一页
                OnPageChanged();//重新去执行重新
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (PageIndex < PageCount)
            {
                PageIndex = PageCount;//翻到最后一页
                OnPageChanged();//重新去执行重新
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            var page = Convert.ToInt32(txtPage.Text.Trim());
            if (page == 0)
            {
                page = 1;
            }
            if (page > PageCount)
            {
                page = PageCount;
            }

            PageIndex = page;
            OnPageChanged();//重新去执行重新
        }

        private void UPager_Load(object sender, EventArgs e)
        {
            InitPageInfo();
        }
    }
}
